package com.wn.consumer.demo06;

import com.rabbitmq.client.Channel;
import com.wn.bean.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * @author yellow Docter
 * @date 2022 -11-03
 * @desc
 */
@Component
@Slf4j
public class ConfirmConsuerm {

    /**
     *
     * @Queue 创建队列的注解
     *      name 队列的名称
     *      arguments 队列属性设置
     *          @Argument 中的 name 是固定的
     *                  x-dead-letter-exchange      队列绑定死信交换机
     *                  x-dead-letter-routing-key   死信路由
     *                  x-message-ttl               消息的过期时间(可以进行计时操作)
     *                  x-max-length                队列的最大长度
     *
     * @param msg
     * @param channel
     * @param message
     */
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "Q_MSG",
                            arguments = {
                                    @Argument(name = "x-dead-letter-exchange",value = "exchange-dlx"),
                                    @Argument(name = "x-dead-letter-routing-key",value = "dlx.ooo"),
                                    @Argument(name = "x-message-ttl",value = "100000",type = "java.lang.Long"),
                                    @Argument(name = "x-max-length",value = "3",type = "java.lang.Long")
                            }
                    ),
                    exchange = @Exchange(name = "direct-msg",type = ExchangeTypes.DIRECT),
                    key = "msg.dead"
            )
    )
    public void handlerMsg(@Payload User user, Channel channel, Message message){

        // @Payload 把接受的json字符串转成 指定的java对象

        log.info("拿到了发送的消息bean:{}",user);

//        long deliveryTag = message.getMessageProperties().getDeliveryTag();
//        //手动拒绝消息
//        try {
//            channel.basicReject(deliveryTag,false);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

}